{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# rStar-Math: Advanced Calculus Examples\n",
    "\n",
    "This notebook demonstrates how rStar-Math handles complex calculus problems."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from src.core.mcts import MCTS\n",
    "from src.core.ppm import ProcessPreferenceModel\n",
    "from src.models.model_interface import ModelFactory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup Components"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "mcts = MCTS.from_config_file('config/default.json')\n",
    "ppm = ProcessPreferenceModel.from_config_file('config/default.json')\n",
    "model = ModelFactory.create_model('openai', os.getenv('OPENAI_API_KEY'), 'config/default.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Derivatives and Integration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "calculus_problems = [\n",
    "    \"Find the derivative of f(x) = sin(x)cos(x)\",\n",
    "    \"Integrate ∫x²e^x dx\",\n",
    "    \"Find the second derivative of f(x) = ln(x)\",\n",
    "    \"Solve the differential equation dy/dx = x + y\"\n",
    "]\n",
    "\n",
    "for problem in calculus_problems:\n",
    "    print(f\"Problem: {problem}\\n\")\n",
    "    action, trajectory = mcts.search(problem)\n",
    "    \n",
    "    print(\"Solution Steps:\")\n",
    "    for step in trajectory:\n",
    "        confidence = ppm.evaluate_step(step['state'], model)\n",
    "        print(f\"- {step['state']}\")\n",
    "        print(f\"  Confidence: {confidence:.2f}\\n\")\n",
    "    print(\"-\" * 50 + \"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Limits and Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "limit_problems = [\n",
    "    \"Find the limit of (1 + 1/n)^n as n approaches infinity\",\n",
    "    \"Find the sum of the infinite series 1 + 1/2 + 1/4 + 1/8 + ...\",\n",
    "    \"Determine if the series Σ(1/n) converges\"\n",
    "]\n",
    "\n",
    "for problem in limit_problems:\n",
    "    print(f\"Problem: {problem}\\n\")\n",
    "    action, trajectory = mcts.search(problem)\n",
    "    \n",
    "    print(\"Solution Steps:\")\n",
    "    for step in trajectory:\n",
    "        confidence = ppm.evaluate_step(step['state'], model)\n",
    "        print(f\"- {step['state']}\")\n",
    "        print(f\"  Confidence: {confidence:.2f}\\n\")\n",
    "    print(\"-\" * 50 + \"\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Visualization of Solutions\n",
    "\n",
    "Let's visualize some of the calculus concepts."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "def plot_function_and_derivative(f_str: str):\n",
    "    \"\"\"Plot a function and its derivative.\"\"\"\n",
    "    problem = f\"Find the derivative of f(x) = {f_str}\"\n",
    "    action, trajectory = mcts.search(problem)\n",
    "    \n",
    "    # Extract derivative from solution\n",
    "    derivative_str = trajectory[-1]['state']\n",
    "    \n",
    "    # Create plot\n",
    "    x = np.linspace(-5, 5, 100)\n",
    "    \n",
    "    # Original function\n",
    "    f = eval(f\"lambda x: {f_str}\")\n",
    "    plt.plot(x, f(x), label=f'f(x) = {f_str}')\n",
    "    \n",
    "    # Derivative\n",
    "    try:\n",
    "        f_prime = eval(f\"lambda x: {derivative_str}\")\n",
    "        plt.plot(x, f_prime(x), '--', label=f\"f'(x) = {derivative_str}\")\n",
    "    except:\n",
    "        print(\"Could not plot derivative\")\n",
    "    \n",
    "    plt.grid(True)\n",
    "    plt.legend()\n",
    "    plt.title(f\"Function and its Derivative\")\n",
    "    plt.show()\n",
    "\n",
    "# Example plots\n",
    "functions = [\n",
    "    \"x**2\",\n",
    "    \"np.sin(x)\",\n",
    "    \"np.exp(x)\"\n",
    "]\n",
    "\n",
    "for f_str in functions:\n",
    "    plot_function_and_derivative(f_str)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
